CommonUtils.regNamespace("com.im.fileTempManage");

com.im.fileTempManage.invoke = function () {
    var param = {"pageInfo":{"pageSize":10,"pageNumber":1}};
    var fileTemp;
    var tmpfiles;
    var file = new com.im.common.file();
    var client = new com.im.common.ajax();
    var filecolumns = [[
        {
            field: 'paramValId', title: "序号",width:$(this).width()*0.03, fitColumns: true,
            formatter: function (value, row, index) {
                return (index + 1);
            }
        },
        {field: 'paramValName', title: "模板名称",width:$(this).width()*0.15, fitColumns: true},
        {field: 'uploadName', title: "文件名称",width:$(this).width()*0.1, fitColumns: true},
        {field: 'fileSuffix', title: "文件后缀",width:$(this).width()*0.1, fitColumns: true},
        {field: 'uploadSize', title: "文件大小(KB)",width:$(this).width()*0.1, fitColumns: true,
            formatter: function (value, row, index) {
                return parseFloat((value/1024).toFixed(2));
            }},
        {field: 'filePath', title: "文件路劲",width:$(this).width()*0.1, fitColumns: true,hidden : true},
        {field: 'uploadId', title: "文件主键",width:$(this).width()*0.1, fitColumns: true,hidden : true},
        {field: 'uploadDate', title: "上传时间",width:$(this).width()*0.1, fitColumns: true},
        {field: 'description', title: "文件描述",width:$(this).width()*0.2, fitColumns: true},
        {
            field: 'operation', title: "操作",width:$(this).width()*0.1, fitColumns: true,
            formatter: function (value, row, index) {
                if(row.uploadId != undefined && row.uploadId != ''){
                    return '<a href="javascript:;" onclick="com.im.fileTempManage.invoke.modifyTempFile(\'' + index + '\',\'modify\')" title="新增文件"><i class="fa fa-edit"></i></a>' +
                        '<a href="javascript:;" onclick="com.im.fileTempManage.invoke.modifyTempFile(\'' + index + '\',\'download\')" title="下载"><i class="fa fa-cloud-download"></i></a>';

                }else{
                    return '<a href="javascript:;" onclick="com.im.fileTempManage.invoke.modifyTempFile(\'' + index + '\',\'modify\')" title="修改文件"><i class="fa fa-edit"></i></a>' +
                        '<a href="javascript:;" onclick="com.im.fileTempManage.invoke.modifyTempFile(\'' + index + '\',\'download\')" title="下载"><i class="fa fa-cloud-download"></i></a>';

                }
            }
        }
    ]];
    function _init() {

        //查询常量值
        _queryFileTemps();

        //保存按钮
        $("#saveBtn").click(function () {
            _updateTempFile();
        });
    }
    function _queryFileTemps(){
        var result = client.callAsJson("commonService/queryBusiConstValByCond", {"constParamCode":"DOC_TEMP_TYPE"});
        if (result.success) {
            //获取文档模板
            $("#fileTempList").datagrid({
        	    nowrap:false,
        	    fitColumns:true,
                columns: filecolumns,
                data: result.result
            });
        }
    }

    function _updateTempFile(){
        //var oparam = {};

        //var fileUploads = [];
        var fileUpload = {};
        if(tmpfiles.newly.length > 0){
            //只要有新增，就覆盖原值
            if(tmpfiles.newly.length > 1){
                toastr.error("您选择的模板问价数目大于1，系统将默认第一个文件上传！！");
            }
            var fileList = [];
            fileList.push(tmpfiles.newly[0]);
            client.callAsFile(fileList, function(result) {
                if(result.success){
                    //fileUpload.paramValId = fileTemp.paramValId;
                    fileUpload.operType = 'add';
                    fileUpload.uploadName = result.result[0].attachFullName;
                    fileUpload.fileSuffix = result.result[0].attachSuffix
                    fileUpload.uploadObjId = fileTemp.paramValId;
                    fileUpload.uploadType = '1';
                    fileUpload.uploadObjType = '80'
                    fileUpload.uploadSize = result.result[0].attachSize;
                    fileUpload.filePath = result.result[0].attachPath;
                    fileUpload.description = $("#fileDesc").val();

                    var res = client.callAsJson("commonService/saveOrUpdateFileTemp", fileUpload);
                    if(res.success){
                        $("#addEditModal").modal("hide");
                        //重新查询
                        _queryFileTemps();
                    }else{
                        toastr.error('服务器忙，文件上传失败！');
                    }
                }else{
                    toastr.error('服务器忙，更新文档类型模板失败！');
                }
            });
        }else{
            //若有有删除，原值置为空
            if(tmpfiles.del.length > 0){
                fileUpload.uploadObjId = fileTemp.paramValId;
                fileUpload.operType = "del";
            }else{
                //更新文件描述
                fileUpload.uploadId = fileTemp.uploadId;
                fileUpload.operType = "mod";
                fileUpload.description = $("#fileDesc").val();

            }
            var res = client.callAsJson("commonService/saveOrUpdateFileTemp", fileUpload);
            if(res.success){
                $("#addEditModal").modal("hide");
                //重新查询
                _queryFileTemps();
            }else{
                toastr.error('服务器忙，更新文档类型模板失败！');
            }

        }

    };

    function _modifyTempFile(idx,operType){

        fileTemp  = $('#fileTempList').datagrid('getData').rows[idx];
        $("#fileDesc").val(fileTemp.description);
        if(operType == 'modify'){
            //初始化数据
            if(fileTemp.uploadId != undefined && fileTemp.uploadId != ''){
                var files = [];
                files.push(fileTemp);
                tmpfiles = file.activate(1,files);
            }else{
                tmpfiles = file.activate(1);
            }
            $("#addEditModal").modal("show");
        }else{
            if(fileTemp.uploadId != undefined && fileTemp.uploadId != ''){
                window.open('/FileManageWeb/fileController/downloadFile?fileName='
                    + fileTemp.filePath +'&realName='+encodeURI(fileTemp.uploadName), '_blank');
            }
        }
    }

    return {
        init: _init,
        modifyTempFile : _modifyTempFile
    }

}();

$(function () {
    com.im.fileTempManage.invoke.init();
});